import { http } from "@/utils/http";
// import { getToken } from "@/utils/auth";
import { getToken175, getToken43 } from "@/utils/token";

type Result = {
  message: string;
  data: any;
  code: number;
};

// 为task API添加Authorization头的辅助函数
const taskRequest = <T>(method: string, url: string, data?: object) => {
  const token = getToken175(); // 每次调用都获取最新的token
  return http.request<T>(method as any, url, {
    data,
    headers: {
      Authorization: token ? `Bearer ${token}` : undefined,
    },
  });
};

const remoteRequest = <T>(method: string, url: string, data?: object) => {
  const remoteToken = getToken43(); // 每次调用都获取最新的token
  return http.request<T>(method as any, url, {
    data,
    headers: {
      Authorization: remoteToken ? `Bearer ${remoteToken}` : undefined,
    },
  });
};

/** 主计划任务列表 */
export const getScheduledTaskData = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/task/scheduled/data", data);
};

/** 编辑主计划任务 */
export const editScheduledTask = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/task/scheduled/edit", data);
};

/** 删除主计划任务 */
export const deleteScheduledTask = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/task/scheduled/delete", data);
};

/** 子节点计划任务列表 */
export const getChildScheduledTaskData = (data?: object) => {
  return remoteRequest<Result>("post", "/api/api2/task/scheduled/data", data);
};

/** 删除子节点计划任务 */
export const deleteChildScheduledTask = (data?: object) => {
  return remoteRequest<Result>("post", "/api/api2/task/scheduled/delete", data);
};
